
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using PaketTrackerApp.PaketTrackerServer;
using System.Net;

namespace PaketTrackerApp
{
	public partial class MainMenu : UIViewController
	{
		#region Constructors

		// The IntPtr and initWithCoder constructors are required for items that need 
		// to be able to be created from a xib rather than from managed code

		public MainMenu (IntPtr handle) : base(handle)
		{
			Initialize ();
		}

		[Export("initWithCoder:")]
		public MainMenu (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public MainMenu () : base("MainMenu", null)
		{
			Initialize ();
		}

		void Initialize ()
		{
		}
		
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			
			txtLogo.ContentMode = UIViewContentMode.ScaleAspectFit;
			txtLogo.Image = UIImage.FromFile("baer.png");
			
			
			//Debug
			txtPaketIdent.Text = "V66V2PC2DT809IM";
			
			txtStatus.Hidden = true;
			txtMap.Hidden = true;
			lblStatus.Hidden = true;
			
			this.bttStatus.TouchDown += HandleBttStatushandleTouchDown;
		}

		void HandleBttStatushandleTouchDown (object sender, EventArgs e)
		{
			txtStatus.Text = "Status unbekannt";
			txtStatus.Hidden = false;
			txtMap.Hidden = false;
			lblStatus.Hidden = false;
			txtPaketIdent.ResignFirstResponder();
			
			PaketTrackerServer.Webservices client = new PaketTrackerServer.Webservices();
			string bla = client.Test("Bla");
			
			
			PaketStatus status = client.GetPaketStatus(txtPaketIdent.Text);
			
			if(status != null)
			{
				txtStatus.Text = status.Status.ToString();
				
				txtMap.ContentMode = UIViewContentMode.ScaleToFill;
				
				WebClient wc = new WebClient();
            	Uri uri = new Uri(status.MapImageUrl);
            	
   	            wc.DownloadFile(uri, "map.png");
            
				txtMap.Image = UIImage.FromFile("map.png");
				
				
			}
			
			
		}
		#endregion
	}
}

